package com.qiguliuxing.dts.admin.web;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.qiguliuxing.dts.admin.dao.test.PackagingTitleDemo;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@RestController
public class TestController {
    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    @PostMapping("/uploadExcel")
    public String uploadExcel(@RequestParam("file") MultipartFile file) {
        // 解析Excel文件并处理数据
        // ...
        return "文件上传成功";
    }

    @GetMapping("/downloadExcel")
    public void downloadExcel(HttpServletResponse response) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Data");

        // 创建Excel文件的内容
        // ...
        logger.info("hahahahhhhhhhhhhhhhhhhhhh");
        response.setHeader("Content-Disposition", "attachment; filename=example.xlsx");
        try (OutputStream out = response.getOutputStream()) {
            workbook.write(out);
        }
    }


    @GetMapping("testExport")
    public String testMqtt(String msg) {
        PackagingTitleDemo ptd = new PackagingTitleDemo();
        ptd.setConcentration(159);
        ptd.setCurrentDate(LocalDateTime.now().toString());
        ptd.setPackagingDate("2024-08-25");
        ptd.setProductBatchNo("2024-08-29");
        PackagingTitleDemo.PackagingData pd;
        List<PackagingTitleDemo.PackagingData> data = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            pd = new PackagingTitleDemo.PackagingData();
            pd.setMl("29");
            pd.setCompany("重庆医科大学附属医院");
            pd.setMBq("293");
            pd.setActualQuantity("23");
            pd.setRemarks("测试导出模板数据");
            pd.setMCi("213");
            pd.setEverMl("1231");
            pd.setDeliverMethod("空运");
            pd.setUseTime("2024-09-11");
            pd.setMCiActive("123");
            pd.setQuantity("99");
            data.add(pd);
        }
        ptd.setData(data);

        // 模板文件路径
        // TODO 修改模板地址
        String templateFilePath = "/Users/chunfalee/work/personal/youle/产品分装清单.xls";
        // 输出文件路径
        // 修改输出文件地址
        String outFilePath = "/Users/chunfalee/work/personal/youle/test.xls";

        // 创建 ExcelWriter 实例
        ExcelWriter excelWriter = EasyExcel
                // 写入到
                .write(outFilePath)
                // 指定模板
                .withTemplate(templateFilePath)
                .build();

        WriteSheet sheet = EasyExcel.writerSheet().build();
        FillConfig fillConfig = FillConfig.builder()
                // 开启填充换行
                .forceNewRow(true)
                .build();
        // 执行填充操作
        excelWriter.fill(ptd, fillConfig, sheet);

        excelWriter.fill(ptd.getData(), fillConfig, sheet);
        // 结束
        excelWriter.finish();
        return msg;
    }
}
